package com.cfec.ipoth_service.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtils {

    @Value("D:/develop/college_learning_Project/Java/education-platform/videos")
    private String uploadPath;

    /**
     * 上传视频
     * @param file 视频文件
     * @return 视频文件存储路径
     */
    public ResultData<String> uploadVideo(MultipartFile file){
        try {
            // 创建上传目录（如果不存在）
            File uploadDir = new File(uploadPath);
            // 检查上传目录是否存在，如果不存在则创建该目录
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            // 生成唯一文件名
            String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
            String filePath = uploadPath + '/' + fileName;

            // 保存文件
            file.transferTo(new File(filePath));

            // 保存到数据库
//            Video video = new Video();
//            video.setName(file.getOriginalFilename());
//            video.setFilePath(filePath);
//            videoRepository.save(video);

//            return ResponseEntity.ok("Video uploaded successfully: " + file.getOriginalFilename());
            return new ResultData<>(200,"Video uploaded successfully",filePath);
        } catch (IOException e) {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
//                    .body("Could not upload the video: " + e.getMessage());
            return new ResultData<>(500,"Could not upload the video",e.getMessage());
        }
    }
}
